<template>
  <div class="user-container">
    <el-row style="margin-bottom: 10px">
      <el-button type="primary" v-on:click="add">{{$t('userList.add')}}</el-button>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      stripe
      :current-page.sync="currentPage"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="ID" align='center'>
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('userList.name')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('userList.userName')" align="center">
        <template slot-scope="scope">
          {{ scope.row.userName }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('userList.role')" align="center" >
        <template slot-scope="scope">
          {{ scope.row.role }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('userList.action')"
      >
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="handleDelete(row)" v-if="row.role !== 'SupperManager'">{{$t('userList.delete')}}</el-button>
          <el-button type="text" size="small" @click="handleModify(row)">{{$t('userList.modify')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :page-sizes="[10, 50]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              style="margin-top: 20px"
              align="center">
      </el-pagination>
    </div>
    <el-dialog
      :title="$t('userList.addTitle')"
      :visible.sync="centerDialogVisible"
      width="80%"
      center>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item :label="$t('userList.name')">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('userList.password')">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item :label="$t('userList.userName')">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">{{$t('userList.cancel')}}</el-button>
        <el-button type="primary" @click="confirmAdd">{{$t('userList.confirm')}}</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="$t('userList.modifyTitle')"
      :visible.sync="modifyCenterDialogVisible"
      width="80%"
      center>
      <el-form ref="modifyF" :model="formModify" label-width="100px">
        <el-form-item label="ID">
          <el-input v-model="formModify.id" disabled></el-input>
        </el-form-item>
        <el-form-item :label="$t('userList.name')">
          <el-input v-model="formModify.name" disabled></el-input>
        </el-form-item>
        <el-form-item :label="$t('userList.password')">
          <el-input v-model="formModify.password"></el-input>
        </el-form-item>
        <el-form-item :label="$t('userList.userName')">
          <el-input v-model="formModify.userName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyCenterDialogVisible = false">{{$t('userList.cancel')}}</el-button>
        <el-button type="primary" @click="confirmModify">{{$t('userList.confirm')}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.user {
  &-container {
    margin: 0 20px;

    .el-form-item {
      margin-bottom: 15px;
    }

  }
}
</style>

<script>
import { getUserList, addUser, deleteUser, modifyUser } from '../../api/user'
import {isEmptyString} from "@/utils/protocol";
import {arrayContains} from "@/utils/util";

export default {
  data() {
    return {
      tableData: [],
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      statusList: ["Normal"],
      total: 0,
      centerDialogVisible: false,
      form: {
        name: '',
        password: '',
        userName: '',
      },
      formModify: {
        id: '',
        name: '',
        password: '',
        userName: '',
        role:""
      },
      modifyCenterDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  computed: {
    admin() {
      return arrayContains(this.$store.state.user.roles, "Manager") || arrayContains(this.$store.state.user.roles, "SuperManager")
    },
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getUserList({
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        statusList: this.statusList
      }).then(dataResponse => {
        console.info(JSON.stringify(dataResponse))

        this.tableData = dataResponse.data.data.filter(item => item.name.toLowerCase() !== "supermanger")
        this.total = dataResponse.data.totalCount
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData(
          {
            pageNo: this.currentPage,
            pageSize: this.pageSize
          }
      )
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData(
          {
            pageNo: this.currentPage,
            pageSize: this.pageSize
          }
      )
    },
    add() {
      this.centerDialogVisible = true
    },
    confirmAdd() {
      const { name, password, userName } = this.form;
      if (isEmptyString(name) || isEmptyString(password) || isEmptyString(userName)) {
        this.$message({
          type: 'error',
          message: this.$i18n.tc("userList.parameterExtTip")
        })
        return;
      }
      if (name.toLowerCase() === 'supermanager') {
        this.$message({
          type: 'error',
          message: this.$i18n.tc("userList.nameExtTip")
        })
        return;
      }
      addUser({ name: name, password:  password, userName: userName, role: "Normal" });
      this.centerDialogVisible = false;
      this.fetchData();
    },
    handleDelete(data) {
      this.$confirm(this.$i18n.tc("userList.deleteTip") + ": " + data.userName, {type: 'warning'}).then(() => {
        deleteUser(data);
        this.centerDialogVisible = false;
        this.fetchData();
        this.$message({
          type: 'success',
          message: this.$i18n.tc("userList.deleteSuccessTip")
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$i18n.tc("userList.deleteCancelTip")
        })
      })
    },
    handleModify(data) {
      this.formModify = data;
      this.modifyCenterDialogVisible = true;
    },
    async confirmModify() {
      const { name, password, userName, id, role } = this.formModify;
      if (isEmptyString(name) || isEmptyString(password) || isEmptyString(userName)) {
        this.$message({
          type: 'error',
          message: this.$i18n.tc("userList.parameterExtTip")
        })
        return;
      }
      if (name.toLowerCase() === 'suppermanager') {
        this.$message({
          type: 'error',
          message: this.$i18n.tc("userList.nameExtTip")
        })
        return;
      }
      await modifyUser({ id: id, name: name, password: password, userName: userName, role: role});
      this.modifyCenterDialogVisible = false;
      this.fetchData()
    }
  }
}
</script>
